package cj.web.admin.dao;

import java.util.List;
import java.util.Map;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;
import cj.web.admin.domain.AuditStep;
import cj.web.admin.domain.AuditStepConfig;

/**
 * 
 * @author 
 *
 */
@DAO
public interface NewsAuditDAO{
	
	//判断是否存在审核
	@SQL("select count(1) from AuditStepConfig where deleteFlag=1")
	int getAuditConfigSize();
	
	//插入审核记录
	@SQL("INSERT INTO AuditStep (newsId,name,stepBegin,stepEnd,userId,deleteFlag,createUser,createAt,updateUser,updateAt,isAgree,memo) VALUES (:t.newsId,:t.name,:t.stepBegin,:t.stepEnd,:t.userId,1,:t.createUser,now(),:t.updateUser,now(),:t.isAgree,:t.memo)")
	void createAuditStep(@SQLParam("t") AuditStep auditStep);
	
	//查询审核人
	@SQL("select * from AuditStepConfig where deleteFlag=1 and stepBegin=:1")
	AuditStepConfig getAuditConfigByStepBegin(int stepBegin);
	
	//查询审核人
	@SQL("SELECT * FROM AuditStepConfig WHERE deleteFlag=1 ORDER BY stepBegin asc ")
	List<AuditStepConfig> getAuditConfigAll();
	
	//查询审核人
	@SQL("SELECT * FROM AuditStepConfig WHERE deleteFlag=1 And stepBegin>:1 ORDER BY stepBegin asc ")
	List<AuditStepConfig> getAuditConfigByDYStepBegin(int stepBegin);
	
	//查询审核人
	@SQL("SELECT * FROM AuditStepConfig WHERE deleteFlag=1 And stepBegin<:1 ORDER BY stepBegin asc ")
	List<AuditStepConfig> getAuditConfigByXYStepBegin(int stepBegin);
	
	//by userId 查询审核记录
	@SQL("select a.id,a.type,a.columnsId,a.title,a.abstractInfor,a.author,a.status,a.releaseState,a.releaseAt,a.label,a.isTop,a.isComments,a.stickAt,a.praiseNum,a.commentsNum,a.collectNum,b.userId,b.stepBegin,b.stepEnd,b.name from News a "
			+ " join AuditStep b on a.id=b.newsId where a.deleteFlag=1 and a.status = 0 and a.releaseState = 1 and b.userId=:1 and b.deleteFlag=1 and b.isAgree=0 #if(:4!=''){ and a.title like '%##(:4)%' } ORDER BY a.releaseState desc,a.createAt desc LIMIT :2,:3 ")
	List<Map> getNewList(int userId,int start,int length,String title);

	//新闻列表
	@SQL("select count(1) from News a join AuditStep b on a.id=b.newsId where a.deleteFlag=1 and a.status = 0 and a.releaseState = 1 and b.userId=:1 and b.deleteFlag=1 and b.isAgree=0 #if(:2!=''){ and a.title like '%##(:2)%' } ")
	int getNewListSize(int userId,String title);
	
	//查询审核记录
	@SQL("select a.id,a.type,a.columnsId,a.title,a.abstractInfor,a.author,a.status,a.releaseState,a.releaseAt,a.label,a.isTop,a.isComments,a.stickAt,a.praiseNum,a.commentsNum,a.collectNum,b.userId,b.stepBegin,b.stepEnd,b.name from News a "
			+ " join AuditStep b on a.id=b.newsId where a.deleteFlag=1 and a.status = 0 and a.releaseState = 1 and a.id=:1 and b.deleteFlag=1 and b.isAgree=0 ")
	List<Map> getNewObjByNewsId(int id);
	
	//查询审核人员
	@SQL("SELECT * FROM AuditStep WHERE deleteFlag=1 AND userId=:1 AND newsId=:2 AND stepBegin=:3 AND isAgree=0")
    AuditStep queryAuditStepById(int userId,int newsId,int stepBegin);

	//更新审核记录
	@SQL("UPDATE AuditStep SET deleteFlag=0,updateAt=now(),isAgree=:2,memo=:3 WHERE id=:1")
	void updateAuditStep(int id,int isAgree,String memo);
	
	//跳过步骤
	@SQL("UPDATE AuditStep SET deleteFlag=0,updateAt=now(),memo=:2 WHERE id=:1")
	void updateAuditStep(int id,String memo);
	
	//更新新闻审核状态与发布状态记录
	@SQL("UPDATE News SET releaseState=:2,status=:3 WHERE id=:1")
	void updateNewsById(int id,int releaseState,int status);
	
	//查询出审核人员列表  < stepBegin
	@SQL("SELECT * FROM AuditStep WHERE newsId=:1 AND stepBegin <:2 AND isAgree=0 and deleteFlag=1")
	List<AuditStep> queryAuditStepXYById(int newsId,int stepBegin);
	
	//查询出审核人员列表  < stepBegin
	@SQL("SELECT * FROM AuditStep WHERE newsId=:1 AND isAgree=0 and deleteFlag=1")
	List<AuditStep> queryAuditStepById(int newsId);
	
	//查找当前审核步骤
	@SQL("select * from AuditStep where newsId = :1 and deleteFlag=0 order by id desc LIMIT 0,1")
	AuditStep queryNewsCurrentAuditStep(int newsId);
}